package com.atguigu.gulimall.cart.vo;

import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车
 *
 * @author LaiSanShan
 * @date 2021-08-27
 */
public class Cart {

    private List<CartItem> items;
    // 商品数量
    private Integer countNum;
    // 商品类型数量
    private Integer countType;
    //商品总价
    private BigDecimal totalAmount;
    // 减免价格
    private BigDecimal reduce = BigDecimal.ZERO;

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    // 计算商品数量
    public Integer getCountNum() {
        if (CollectionUtils.isEmpty(items)) {
            return 0;
        }
        return items.stream().map(CartItem::getCount).reduce(0, (a, b) -> a + b);
    }

    public void setCountNum(Integer countNum) {
        this.countNum = countNum;
    }

    public void setCountType(Integer countType) {
        this.countType = countType;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    // 计算商品类型数量
    public Integer getCountType() {
        return items.size();
    }

    // 计算商品总价
    public BigDecimal getTotalAmount() {
        BigDecimal count = items.stream().filter(CartItem::getCheck).map(CartItem::getTotalPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
        return count.subtract(this.reduce);
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }

    /**
     * 购物项
     */
    public static class CartItem {
        private Long skuId;
        private Boolean check = true;
        private String title;
        private String image;
        private List<String> skuAttr;
        private BigDecimal price;
        private Integer count;
        private BigDecimal totalPrice;

        public Long getSkuId() {
            return skuId;
        }

        public void setSkuId(Long skuId) {
            this.skuId = skuId;
        }

        public Boolean getCheck() {
            return check;
        }

        public void setCheck(Boolean check) {
            this.check = check;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }

        public List<String> getSkuAttr() {
            return skuAttr;
        }

        public void setSkuAttr(List<String> skuAttr) {
            this.skuAttr = skuAttr;
        }

        public BigDecimal getPrice() {
            return price;
        }

        public void setPrice(BigDecimal price) {
            this.price = price;
        }

        public Integer getCount() {
            return count;
        }

        public void setCount(Integer count) {
            this.count = count;
        }

        /**
         * 计算总价
         *
         * @return 单价x数量
         */
        public BigDecimal getTotalPrice() {
            return this.price.multiply(new BigDecimal(this.count.toString()));
        }

        public void setTotalPrice(BigDecimal totalPrice) {
            this.totalPrice = totalPrice;
        }
    }

}
